package club.justwill.single.Service.impl;

import club.justwill.single.Service.OrderService;
import club.justwill.single.Service.ProjectIntroduceService;
import club.justwill.single.assembler.OrderAssemble;
import club.justwill.single.bean.*;
import club.justwill.single.helper.FileHelper;
import club.justwill.single.model.CoreConstants;
import club.justwill.single.model.JsonResponse;
import club.justwill.single.model.SecurityToken;
import club.justwill.single.model.UserType;
import club.justwill.single.persistence.dao.OrderDao;
import club.justwill.single.persistence.dao.ProjectDao;
import club.justwill.single.persistence.dao.ProjectIntroduceDao;
import club.justwill.single.persistence.dao.SkuDao;
import club.justwill.single.persistence.jpo.*;
import club.justwill.single.util.JsonResponseHelper;
import club.justwill.single.util.ProjectHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luoyu on 15-12-27.
 */
@Service
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class ProjectIntroduceServiceImpl extends BaseService implements ProjectIntroduceService {

    @Autowired
    private ProjectIntroduceDao projectIntrodeceDao;

    @Autowired
    private ProjectDao projectDao;

    @Autowired
    private FileHelper fileHelper;

    @Autowired
    private OrderService orderService;

    @Autowired
    private SkuDao skuDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Throwable.class, readOnly = false)
    public ProjectIntroduce addProjectIntroduce(SecurityToken token, CreateProjectIntroduce createProjectIntroduce, JsonResponse obj) {
        if (projectDao.getByProjectId(createProjectIntroduce.getProjectId()) == null) {
            JsonResponseHelper.addError(obj, CoreConstants.PROJECT_NOT_EXIST);
            return new ProjectIntroduce();
        }
        if(token.getUserType() == UserType.ANONYMOUS || token.getUserType() == UserType.INDIVIDUAL){
            JsonResponseHelper.addError(obj, CoreConstants.PROJECT_INTRODUCE_CREATE_ERROR);
            return new ProjectIntroduce();
        }
        if (ProjectHelper.canCreateProjectIntroduce(createProjectIntroduce, obj)) {
            ProjectIntroduce projectsIntroduce = new ProjectIntroduce();
            projectsIntroduce.setProjectId(createProjectIntroduce.getProjectId());
            projectsIntroduce.setType(createProjectIntroduce.getType());
            projectsIntroduce.setContent(createProjectIntroduce.getContent());
            projectsIntroduce.setC_type(createProjectIntroduce.getC_type());
            projectsIntroduce.setTum(createProjectIntroduce.getTum());
            projectIntrodeceDao.save(projectsIntroduce, token);
            return projectsIntroduce;
        }
        return null;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Throwable.class, readOnly = false)
    public List<ProjectIntroduce> searchProjectIntroduce(SearchProjectIntroduce searchProjectIntroduce, JsonResponse obj) {

            if(searchProjectIntroduce.getIndex()<0|| searchProjectIntroduce.getSize()<=0) {
                JsonResponseHelper.addError(obj,CoreConstants.PROJECT_INTRODUCE_ARGUMENT_ERROR);
                return null;
            } else {
                List<ProjectIntroduce> introduceList = projectIntrodeceDao.searchProjectIntroduce(searchProjectIntroduce.getProjectId(),
                        searchProjectIntroduce.getIndex(),
                        searchProjectIntroduce.getSize());
                return introduceList;
            }


    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Throwable.class, readOnly = false)
    public List<ProjectIntroduce> searchProjectSupportReturn(long projectId, JsonResponse obj) {
        List<ProjectIntroduce> supportReturnList = projectIntrodeceDao.searchProjectSupportReturn(projectId);
       /* if(supportReturnList == null || supportReturnList.size() == 0) {
            JsonResponseHelper.addError(obj, CoreConstants.PROJECT_INTRODUCE_IS_EMPTY);
        }*/
        return supportReturnList;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Throwable.class, readOnly = false)
    public void saveFile(SecurityToken token, JsonResponse obj, CommonsMultipartFile cover,Long introduceId) {
        String path = fileHelper.saveFile(cover);
        if(path == null || "".equals(path)){
            JsonResponseHelper.addError(obj, CoreConstants.FILE_UPLOAD_FAILED);
        }else{
            ProjectIntroduce projectIntroduce = projectIntrodeceDao.searchByIntroduceId(introduceId);
            projectIntroduce.setContent(path);
            projectIntrodeceDao.update(projectIntroduce,token);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Throwable.class, readOnly = false)
    public void updateProjectIntroduce(SecurityToken token, UpdateProjectIntroduce updateProjectIntroduce, JsonResponse obj) {
        if(token.getUserType() == UserType.ANONYMOUS || token.getUserType() == UserType.INDIVIDUAL){
            JsonResponseHelper.addError(obj, CoreConstants.PROJECT_INTRODUCE_CREATE_ERROR);
        }
        if (ProjectHelper.canUpdateProjectIntroduce(updateProjectIntroduce, obj)) {
            ProjectIntroduce introduce = projectIntrodeceDao.searchByIntroduceId(updateProjectIntroduce.getIntroduceId());
            if(introduce !=null){
                introduce.setType(updateProjectIntroduce.getType());
                introduce.setContent(updateProjectIntroduce.getContent());
                introduce.setC_type(updateProjectIntroduce.getC_type());
                introduce.setTum(updateProjectIntroduce.getTum());
                projectIntrodeceDao.save(introduce, token);
            }
        }
    }

}
